home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Rectangle.java < prev    next >
Text File  |  1998-09-22  |  20KB  |  560 lines

  1. /*
  2.  * @(#)Rectangle.java    1.28 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt;
  16.  
  17. /**
  18.  * A rectangle specifies an area in a coordinate space that is 
  19.  * defined by the rectangle's top-left point (<i>x</i>, <i>y</i>) 
  20.  * in the coordinate space, its width, and its height. 
  21.  * <p>
  22.  * A rectangle's <code>width</code> and <code>height</code> are 
  23.  * public fields. The constructors that allow you to create a 
  24.  * rectangle, and the methods that allow you to modify one, do not 
  25.  * prevent you from setting a negative value for width or height. 
  26.  * <p>
  27.  * A rectangle whose width or height is negative is considered 
  28.  * empty, and all methods defined by the <code>Rectangle</code> class  
  29.  * behave accordingly. If the rectangle is empty, then the method 
  30.  * <code>isEmpty</code> returns <code>true</code>. No point can be 
  31.  * contained by or inside an empty rectangle, however the values of 
  32.  * <code>width</code> and <code>height</code> are still valid. An 
  33.  * empty rectangle still has a location in the coordinate space, and 
  34.  * methods that change its size or location remain valid. The 
  35.  * behavior of methods that operate on more than one rectangle is 
  36.  * undefined if any of the participating rectangles has a negative 
  37.  * <code>width</code> or <code>height</code>. These methods include 
  38.  * <code>intersects</code>, <code>intersection</code>, and 
  39.  * <code>union</code>. 
  40.  *
  41.  * @version     1.28, 07/01/98
  42.  * @author     Sami Shaio
  43.  * @since       JDK1.0
  44.  */
  45. public class Rectangle implements Shape, java.io.Serializable {
  46.  
  47.     /**
  48.      * The <i>x</i> coordinate of the rectangle.
  49.      * @since     JDK1.0
  50.      */
  51.     public int x;
  52.  
  53.     /**
  54.      * The <i>y</i> coordinate of the rectangle.
  55.      * @since     JDK1.0
  56.      */
  57.     public int y;
  58.  
  59.     /**
  60.      * The width of the rectangle.
  61.      * @since     JDK1.0.
  62.      */
  63.     public int width;
  64.  
  65.     /**
  66.      * The height of the rectangle.
  67.      * @since     JDK1.0
  68.      */
  69.     public int height;
  70.  
  71.     /*
  72.      * JDK 1.1 serialVersionUID 
  73.      */
  74.      private static final long serialVersionUID = -4345857070255674764L;
  75.  
  76.     /**
  77.      * Constructs a new rectangle whose top-left corner is at (0, 0) 
  78.      * in the coordinate space, and whose width and height are zero. 
  79.      * @since     JDK1.0
  80.      */
  81.     public Rectangle() {
  82.         this(0, 0, 0, 0);
  83.     }
  84.  
  85.     /**
  86.      * Constructs a new rectangle, initialized to match the values of
  87.      * the specificed rectangle.
  88.      * @param r  a rectangle from which to copy initial values.
  89.      * @since JDK1.1
  90.      */
  91.     public Rectangle(Rectangle r) {
  92.         this(r.x, r.y, r.width, r.height);
  93.     }
  94.  
  95.     /**
  96.      * Constructs a new rectangle whose top-left corner is specified as
  97.      * (<code>x</code>, <code>y</code>) and whose width and height 
  98.      * are specified by the arguments of the same name. 
  99.      * @param     x   the <i>x</i> coordinate.
  100.      * @param     y   the <i>y</i> coordinate.
  101.      * @param     width    the width of the rectangle.
  102.      * @param     height   the height of the rectangle.
  103.      * @since     JDK1.0
  104.      */
  105.     public Rectangle(int x, int y, int width, int height) {
  106.     this.x = x;
  107.     this.y = y;
  108.     this.width = width;
  109.     this.height = height;
  110.     }
  111.  
  112.     /**
  113.      * Constructs a new rectangle whose top-left corner is at (0, 0) 
  114.      * in the coordinate space, and whose width and height are specified 
  115.      * by the arguments of the same name. 
  116.      * @param     width    the width of the rectangle.
  117.      * @param     height   the height of the rectangle.
  118.      * @since     JDK1.0
  119.      */
  120.     public Rectangle(int width, int height) {
  121.     this(0, 0, width, height);
  122.     }
  123.  
  124.     /**
  125.      * Constructs a new rectangle whose top-left corner is specified 
  126.      * by the <code>point</code> argument, and whose width and height  
  127.      * are specified by the <code>dimension</code> argument. 
  128.      * @param     p   a point, the top-left corner of the rectangle.
  129.      * @param     d   a dimension, representing the width and height.
  130.      * @since     JDK1.0 
  131.      */
  132.     public Rectangle(Point p, Dimension d) {
  133.     this(p.x, p.y, d.width, d.height);
  134.     }
  135.     
  136.     /**
  137.      * Constructs a new rectangle whose top-left corner is the  
  138.      * specified point, and whose width and height are zero. 
  139.      * @param     p   the top left corner of the rectangle.
  140.      * @since     JDK1.0
  141.      */
  142.     public Rectangle(Point p) {
  143.     this(p.x, p.y, 0, 0);
  144.     }
  145.     
  146.     /**
  147.      * Constructs a new rectangle whose top left corner is  
  148.      * (0, 0) and whose width and height are specified  
  149.      * by the <code>dimension</code> argument. 
  150.      * @param     d   a dimension, specifying width and height.
  151.      * @since     JDK1.0
  152.      */
  153.     public Rectangle(Dimension d) {
  154.     this(0, 0, d.width, d.height);
  155.     }
  156.  
  157.     /**
  158.      * Gets the bounding rectangle of this rectangle.
  159.      * <p>
  160.      * This method is included for completeness, to parallel the
  161.      * <code>getBounds</code> method of <code>Component</code>.
  162.      * @return    a new rectangle, equal to the bounding rectangle 
  163.      *                for this rectangle.
  164.      * @see       java.awt.Component#getBounds
  165.      * @since     JDK1.1
  166.      */
  167.     public Rectangle getBounds() {
  168.     return new Rectangle(x, y, width, height);
  169.     }    
  170.  
  171.     /**
  172.      * Sets the bounding rectangle of this rectangle to match 
  173.      * the specified rectangle.
  174.      * <p>
  175.      * This method is included for completeness, to parallel the
  176.      * <code>setBounds</code> method of <code>Component</code>.
  177.      * @param     r   a rectangle.
  178.      * @see       java.awt.Component#setBounds(java.awt.Rectangle)
  179.      * @since     JDK1.1
  180.      */
  181.     public void setBounds(Rectangle r) {
  182.     setBounds(r.x, r.y, r.width, r.height);
  183.     }    
  184.  
  185.     /**
  186.      * Sets the bounding rectangle of this rectangle to the specified 
  187.      * values for <code>x</code>, <code>y</code>, <code>width</code>, 
  188.      * and <code>height</code>.
  189.      * <p>
  190.      * This method is included for completeness, to parallel the
  191.      * <code>setBounds</code> method of <code>Component</code>.
  192.      * @param     x       the new <i>x</i> coordinate for the top-left
  193.      *                    corner of this rectangle.
  194.      * @param     y       the new <i>y</i> coordinate for the top-left
  195.      *                    corner of this rectangle.
  196.      * @param     width   the new width for this rectangle.
  197.      * @param     height  the new height for this rectangle.
  198.      * @see       java.awt.Component#setBounds(int, int, int, int)
  199.      * @since     JDK1.1
  200.      */
  201.     public void setBounds(int x, int y, int width, int height) {
  202.         reshape(x, y, width, height);
  203.     }    
  204.  
  205.     /**
  206.      * @deprecated As of JDK version 1.1,
  207.      * replaced by <code>setBounds(int, int, int, int)</code>.
  208.      */
  209.     public void reshape(int x, int y, int width, int height) {
  210.     this.x = x;
  211.     this.y = y;
  212.     this.width = width;
  213.     this.height = height;
  214.     }    
  215.  
  216.     /**
  217.      * Returns the location of this rectangle.
  218.      * <p>
  219.      * This method is included for completeness, to parallel the
  220.      * <code>getLocation</code> method of <code>Component</code>.
  221.      * @see       java.awt.Component#getLocation
  222.      * @since     JDK1.1
  223.      */
  224.     public Point getLocation() {
  225.     return new Point(x, y);
  226.     }    
  227.  
  228.     /**
  229.      * Moves the rectangle to the specified location.
  230.      * <p>
  231.      * This method is included for completeness, to parallel the
  232.      * <code>setLocation</code> method of <code>Component</code>.
  233.      * @param     p  the new location for the point.
  234.      * @see       java.awt.Component#setLocation(java.awt.Point)
  235.      * @since     JDK1.1
  236.      */
  237.     public void setLocation(Point p) {
  238.     setLocation(p.x, p.y);
  239.     }    
  240.  
  241.     /**
  242.      * Moves the rectangle to the specified location.
  243.      * <p>
  244.      * This method is included for completeness, to parallel the
  245.      * <code>setLocation</code> method of <code>Component</code>.
  246.      * @param     x  the <i>x</i> coordinate of the new location.
  247.      * @param     y  the <i>y</i> coordinate of the new location.
  248.      * @see       java.awt.Component#setLocation(int, int)
  249.      * @since     JDK1.1
  250.      */
  251.     public void setLocation(int x, int y) {
  252.     move(x, y);
  253.     }    
  254.  
  255.     /**
  256.      * @deprecated As of JDK version 1.1,
  257.      * replaced by <code>setLocation(int, int)</code>.
  258.      */
  259.     public void move(int x, int y) {
  260.     this.x = x;
  261.     this.y = y;
  262.     }    
  263.  
  264.     /**
  265.      * Translates the rectangle the indicated distance,
  266.      * to the right along the <i>x</i> coordinate axis, and 
  267.      * downward along the <i>y</i> coordinate axis.
  268.      * @param     dx   the distance to move the rectangle 
  269.      *                 along the <i>x</i> axis.
  270.      * @param     dy   the distance to move the rectangle 
  271.      *                 along the <i>y</i> axis.
  272.      * @see       java.awt.Rectangle#setLocation(int, int)
  273.      * @see       java.awt.Rectangle#setLocation(java.awt.Point)
  274.      * @since     JDK1.0
  275.      */
  276.     public void translate(int x, int y) {
  277.     this.x += x;
  278.     this.y += y;
  279.     }    
  280.  
  281.     /**
  282.      * Gets the size (width and height) of this rectangle.
  283.      * <p>
  284.      * This method is included for completeness, to parallel the
  285.      * <code>getSize</code> method of <code>Component</code>.
  286.      * @return    a dimension, representing the size.
  287.      * @see       java.awt.Component#getSize
  288.      * @since     JDK1.1
  289.      */
  290.     public Dimension getSize() {
  291.     return new Dimension(width, height);
  292.     }    
  293.  
  294.     /**
  295.      * Sets the size of this rectangle to match the specified dimension.
  296.      * <p>
  297.      * This method is included for completeness, to parallel the
  298.      * <code>setSize</code> method of <code>Component</code>.
  299.      * @param d  the new size for the Dimension object
  300.      * @see       java.awt.Component#setSize(java.awt.Dimension)
  301.      * @since     JDK1.1
  302.      */
  303.     public void setSize(Dimension d) {
  304.     setSize(d.width, d.height);
  305.     }    
  306.  
  307.     /**
  308.      * Sets the size of this rectangle to the specified width and height.
  309.      * <p>
  310.      * This method is included for completeness, to parallel the
  311.      * <code>setSize</code> method of <code>Component</code>.
  312.      * @param     width    the new width for this rectangle object.
  313.      * @param     height   the new height for this rectangle object.
  314.      * @see       java.awt.Component#setSize(int, int)
  315.      * @since     JDK1.1
  316.      */
  317.     public void setSize(int width, int height) {
  318.         resize(width, height);
  319.     }    
  320.  
  321.     /**
  322.      * @deprecated As of JDK version 1.1,
  323.      * replaced by <code>setSize(int, int)</code>.
  324.      */
  325.     public void resize(int width, int height) {
  326.     this.width = width;
  327.     this.height = height;
  328.     }    
  329.  
  330.     /**
  331.      * Checks whether this rectangle contains the specified point.
  332.      * @param p the point (location) to test.
  333.      * @return    <code>true</code> if the point 
  334.      *            (<i>x</i>, <i>y</i>) is inside this rectangle; 
  335.      *            <code>false</code> otherwise.
  336.      * @since     JDK1.1
  337.      */
  338.     public boolean contains(Point p) {
  339.     return contains(p.x, p.y);
  340.     }
  341.  
  342.     /**
  343.      * Checks whether this rectangle contains the point
  344.      * at the specified location (<i>x</i>, <i>y</i>).
  345.      * @param     x   the <i>x</i> coordinate.
  346.      * @param     y   the <i>y</i> coordinate.
  347.      * @return    <code>true</code> if the point 
  348.      *            (<i>x</i>, <i>y</i>) is inside this rectangle; 
  349.      *            <code>false</code> otherwise.
  350.      * @since     JDK1.1
  351.      */
  352.     public boolean contains(int x, int y) {
  353.     return inside(x, y);
  354.     }
  355.  
  356.     /**
  357.      * @deprecated As of JDK version 1.1,
  358.      * replaced by <code>contains(int, int)</code>.
  359.      */
  360.     public boolean inside(int x, int y) {
  361.     return (x >= this.x) && ((x - this.x) < this.width) && (y >= this.y) && ((y-this.y) < this.height);
  362.     }
  363.  
  364.     /**
  365.      * Determines whether this rectangle and the specified rectangle  
  366.      * intersect. Two rectangles intersect if their intersection is 
  367.      * nonempty. 
  368.      * @param     r   a rectangle.
  369.      * @return    <code>true</code> if the specified rectangle 
  370.      *            and this rectangle insersect; 
  371.      *            <code>false</code> otherwise.
  372.      * @since     JDK1.0
  373.      */
  374.     public boolean intersects(Rectangle r) {
  375.     return !((r.x + r.width <= x) ||
  376.          (r.y + r.height <= y) ||
  377.          (r.x >= x + width) ||
  378.          (r.y >= y + height));
  379.     }
  380.  
  381.     /**
  382.      * Computes the intersection of this rectangle with the 
  383.      * specified rectangle. Returns a new rectangle that 
  384.      * represents the intersection of the two rectangles.
  385.      * @param     r   a rectangle.
  386.      * @return    the largest rectangle contained in both the 
  387.      *            specified rectangle and in this rectangle.
  388.      * @since   JDK1.0
  389.      */
  390.     public Rectangle intersection(Rectangle r) {
  391.     int x1 = Math.max(x, r.x);
  392.     int x2 = Math.min(x + width, r.x + r.width);
  393.     int y1 = Math.max(y, r.y);
  394.     int y2 = Math.min(y + height, r.y + r.height);
  395.     return new Rectangle(x1, y1, x2 - x1, y2 - y1);
  396.     }
  397.  
  398.     /**
  399.      * Computes the union of this rectangle with the 
  400.      * specified rectangle. Returns a new rectangle that 
  401.      * represents the union of the two rectangles.
  402.      * @param     r   a rectangle.
  403.      * @return    the smallest rectangle containing both the specified 
  404.      *            rectangle and this rectangle.
  405.      * @since     JDK1.0
  406.      */
  407.     public Rectangle union(Rectangle r) {
  408.     int x1 = Math.min(x, r.x);
  409.     int x2 = Math.max(x + width, r.x + r.width);
  410.     int y1 = Math.min(y, r.y);
  411.     int y2 = Math.max(y + height, r.y + r.height);
  412.     return new Rectangle(x1, y1, x2 - x1, y2 - y1);
  413.     }
  414.  
  415.     /**
  416.      * Adds a point, specified by the integer arguments <code>newx</code>
  417.      * and <code>newy</code>, to this rectangle. The resulting rectangle is
  418.      * the smallest rectangle that contains both the original rectangle 
  419.      * and the specified point.
  420.      * <p>
  421.      * After adding a point, a call to <code>contains<code> with the 
  422.      * added point as an argument will not necessarily return 
  423.      * <code>true</code>. The <code>contains</code> method does not 
  424.      * return <code>true</code> for points on the right or bottom 
  425.      * edges of a rectangle. Therefore if the added point falls on 
  426.      * the left or bottom edge of the enlarged rectangle, 
  427.      * <code>contains</code> will return <code>false</code> for that point.
  428.      * 
  429.      * @param     newx   the <i>x</i> coordinate of the new point.
  430.      * @param     newy   the <i>y</i> coordinate of the new point.
  431.      * @since     JDK1.0
  432.      */
  433.     public void add(int newx, int newy) {
  434.     int x1 = Math.min(x, newx);
  435.     int x2 = Math.max(x + width, newx);
  436.     int y1 = Math.min(y, newy);
  437.     int y2 = Math.max(y + height, newy);
  438.     x = x1;
  439.     y = y1;
  440.     width = x2 - x1;
  441.     height = y2 - y1;
  442.     }
  443.  
  444.     /**
  445.      * Adds the point <code>pt</code> to this rectangle. The resulting 
  446.      * rectangle is the smallest rectangle that contains both the 
  447.      * original rectangle and the specified point.
  448.      * <p>
  449.      * After adding a point, a call to <code>contains<code> with the 
  450.      * added point as an argument will not necessarily return 
  451.      * <code>true</code>. The <code>contains</code> method does not 
  452.      * return <code>true</code> for points on the right or bottom 
  453.      * edges of a rectangle. Therefore if the added point falls on 
  454.      * the left or bottom edge of the enlarged rectangle, 
  455.      * <code>contains</code> will return <code>false</code> for that point.
  456.      * 
  457.      * @param     pt the new point to add to the rectangle.
  458.      * @since     JDK1.0
  459.      */
  460.     public void add(Point pt) {
  461.     add(pt.x, pt.y);
  462.     }
  463.  
  464.     /**
  465.      * Adds a rectangle to this rectangle. The resulting rectangle is
  466.      * the union of the two rectangles. 
  467.      * @param     a rectangle.
  468.      * @since     JDK1.0
  469.      */
  470.     public void add(Rectangle r) {
  471.     int x1 = Math.min(x, r.x);
  472.     int x2 = Math.max(x + width, r.x + r.width);
  473.     int y1 = Math.min(y, r.y);
  474.     int y2 = Math.max(y + height, r.y + r.height);
  475.     x = x1;
  476.     y = y1;
  477.     width = x2 - x1;
  478.     height = y2 - y1;
  479.     }
  480.  
  481.     /**
  482.      * Grows the rectangle both horizontally and vertically.
  483.      * <p>
  484.      * This method modifies the rectangle so that it is 
  485.      * <code>h</code> units larger on both the left and right side, 
  486.      * and <code>v</code> units larger at both the top and bottom. 
  487.      * <p>
  488.      * The new rectangle has (<code>x - h</code>, 
  489.      * <code>y - v</code>) as its top-left corner, a 
  490.      * width of 
  491.      * <code>width</code> <code>+</code> <code>2h</code>, 
  492.      * and a height of 
  493.      * <code>height</code> <code>+</code> <code>2v</code>. 
  494.      * <p>
  495.      * If negative values are supplied for <code>h</code> and 
  496.      * <code>v</code>, the size of the rectangle decreases accordingly. 
  497.      * The <code>grow</code> method does not check whether the resulting 
  498.      * values of <code>width</code> and <code>height</code> are 
  499.      * non-negative. 
  500.      * @param     h   the horizontal expansion.
  501.      * @param     v   the vertical expansion.
  502.      * @since     JDK1.0
  503.      */
  504.     public void grow(int h, int v) {
  505.     x -= h;
  506.     y -= v;
  507.     width += h * 2;
  508.     height += v * 2;
  509.     }
  510.  
  511.     /**
  512.      * Determines whether this rectangle is empty. A rectangle is empty if 
  513.      * its width or its height is less than or equal to zero. 
  514.      * @return     <code>true</code> if this rectangle is empty; 
  515.      *             <code>false</code> otherwise.
  516.      * @since      JDK1.0
  517.      */
  518.     public boolean isEmpty() {
  519.     return (width <= 0) || (height <= 0);
  520.     }
  521.  
  522.     /**
  523.      * Returns the hashcode for this rectangle.
  524.      * @return     the hashcode for this rectangle.
  525.      * @since      JDK1.0
  526.      */
  527.     public int hashCode() {
  528.     return x ^ (y*37) ^ (width*43) ^ (height*47);
  529.     }
  530.  
  531.     /**
  532.      * Checks whether two rectangles are equal.
  533.      * <p>
  534.      * The result is <kbd>true</kbd> if and only if the argument is not 
  535.      * <kbd>null</kbd> and is a <kbd>Rectangle</kbd> object that has the 
  536.      * same top-left corner, width, and height as this rectangle. 
  537.      * @param     obj   the object to compare with.
  538.      * @return    <code>true</code> if the objects are equal; 
  539.      *            <code>false</code> otherwise.
  540.      * @since     JDK1.0
  541.      */
  542.     public boolean equals(Object obj) {
  543.     if (obj instanceof Rectangle) {
  544.         Rectangle r = (Rectangle)obj;
  545.         return (x == r.x) && (y == r.y) && (width == r.width) && (height == r.height);
  546.     }
  547.     return false;
  548.     }
  549.  
  550.     /**
  551.      * Returns a string representation of this rectangle 
  552.      * and its values.
  553.      * @return     a string representation of this rectangle.
  554.      * @since      JDK1.0
  555.      */
  556.     public String toString() {
  557.     return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]";
  558.     }
  559. }
  560.